// pages/balanceRecharge/index.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[1,2,3,4,5,6,7,8,9,0,'.'],
    number:'',
    columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
    isPopup:false,
    noCard:false,
    storeInfo:{},
    msg:'',
    ischeck:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeUserHome()
  },
  onClick(e){
    let number = this.data.number
    number = number+e.currentTarget.dataset.item
    if(Number(number<=100000)){
      number = number
                  .replace(/^\./g, '')
                  .replace('.', '$#$')
                  .replace(/\./g, '')
                  .replace('$#$', '.')
                  .replace(/^00$/, '0')
                  .replace(/^0(\d)/g, '$1')
                  .replace(/^(-)*(\d+)\.(\d{2}).*$/, '$1$2.$3')
          this.setData({
            number:number
          })

          this.checkMoney()
    }
  },
  delete(){
    let number = this.data.number
    number = number.slice(0,number.length-1)
    this.setData({
      number:number
    })
    this.checkMoney()
  },

  allIn(){
    this.setData({
      number:this.data.userInfo.withdrawable
    })
    this.checkMoney()
  },

  checkMoney:function(){
    var number=this.data.number
    var userInfo=this.data.userInfo
    if(Number(number)<=0){
      this.setData({
        ischeck:false,
        msg:''
      })
    }else if(Number(number)>Number(userInfo.withdrawable)){
      this.setData({
        msg:'输入金额超出可提现金额',
        ischeck:false
      })
    }else{
      this.setData({
        msg:'',
        ischeck:true
      })
    }
  },

  openPopup(){
    this.setData({
      isPopup:true
    })
  },
  onConfirm(event) {
    this.setData({
      isPopup:false
    })
  },
  onCancel() {
    this.setData({
      isPopup:false
    })
  },
  addCard(){
    wx.navigateTo({
      url: '/pages/addCard/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  storeUserHome(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeUserHome(data).then(res=>{
      console.log(res)
      var datas=res.data
      var balance=datas.balance
      var freezePrice=datas.freezePrice
      var withdrawal=datas.withdrawal
      datas.balance=balance.toFixed(2)
      datas.withdrawal=withdrawal.toFixed(2)
      datas.withdrawable=Number(Number(balance)-Number(freezePrice)).toFixed(2)
      this.setData({
        userInfo:datas
      })
      wx.setStorageSync('storeInfo', res.data)
    })
  },
  sure:function(){
    var that=this
    var number=this.data.number
    //var userInfo=this.data.userInfo
    var data={}
    data.price=number
  
    data.storeId=wx.getStorageSync('userInfo').storeId
    app.agriknow.storeWithdrawal(data).then(res=>{
      console.log(res)
      if(res.code=='000000'){
        wx.showToast({
          title: '提现申请成功，等待审核',
          icon:'none',
          duration:1500,
          mask:true
        })
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/withdrawalDetails/index',
          })
        },1500)
      }else{
        wx.showToast({
          title: res.data,
          icon:'none',
          duration:1500,
          mask:true
        })
        
      }
    })
  }
})